package com.xauto.pioneer.appuser.activity.mine.user;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.common.TakePhotoActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.Image;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.DataCheckUtil;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.util.TimeUtil;
import com.xauto.pioneer.appuser.util.ToastUtil;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;

import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.DO_BIG_IMAGE;
import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.UPLOAD_IMG;

//import static com.xauto.pioneer.appuser.R.id.LtvIdInfoTakePhoto;

//import static com.xauto.pioneer.appuser.R.id.tvHalfBodyIdInfoTakePhoto;
//import static com.xauto.pioneer.appuser.R.id.tvIdInfoTakePhoto;


/***
 * 身份证信息认证页面
 * @author Lemon Thu 23 Feb 19:21
 */
public class IdentificationActivity extends BaseActivity {
    private static  String IDENTIFICATION_AUTH_STATUS = "002";
    private static  String IDENTIFICATION_WITH_PEOPLE_AUTH_STATUS = "003";
    @Bind(R.id.tvHalfBodyIdInfoTakePhoto) TextView tvHalfBodyIdInfoTakePhoto;
    @Bind(R.id.tvIdInfoTakePhoto) TextView tvIdInfoTakePhoto;
    @Bind(R.id.topBar) TopBar topBar;
    /** 显示-身份证*/
    @Bind(R.id.ivIdInfo) ImageView ivIdInfo;
    /** 点击-下一步按钮*/
    @Bind(R.id.tvIdInfoNext) TextView tvIdInfoNext;
    /** 身份证界面*/
    @Bind(R.id.layoutVerifyIdentity) View layoutVerifyIdentity;
    /** 手持身份证界面*/
    @Bind(R.id.layoutVerifyHalfBodyIdentity) View layoutVerifyHalfBodyIdentity;
    /** 审核中界面*/
    @Bind(R.id.layoutPromptStatus) View layoutPromptStatus;
    /** 审核成功界面*/
    @Bind(R.id.layoutPromptSuccess) View layoutPromptSuccess;
    /** 审核成功界面-身份证*/
    @Bind(R.id.ivDriverSuccess) ImageView ivDriverSuccess;
    /** 审核成功界面-手持身份证身份证*/
    @Bind(R.id.ivDriverWithPeopleSuccess) ImageView ivDriverWithPeopleSuccess;
    /** 显示-半身照图片 */
    @Bind(R.id.ivHalfBodyIdInfo) ImageView ivHalfBodyIdInfo;
    /** 按钮效果-半身照下一步*/
    @Bind(R.id.tvHalfBodyIdInfoNext) TextView tvHalfBodyIdInfoNext;
    private PromptDialog promptDialog;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private String imageUrl;
    //0-未认证 1-已认证 2-认证中
    private int identificationAuthStatus;
    private int identificationWithPeopleAuthStatus;

    /** 选择本地图片路径*/
    private String currentImgLocalPath = "";
    private String currentImgUploadType;
    /** 身份证上传路径*/
    private String identificationUploadPath = "";
    /**
     * 入口
     *
     * @param mContext
     */
    public static void startAction(Context mContext, int... types) {
        Intent intent = new Intent(mContext, IdentificationActivity.class);
        intent.putExtra(IDENTIFICATION_AUTH_STATUS, types[0]);
        intent.putExtra(IDENTIFICATION_WITH_PEOPLE_AUTH_STATUS, types[1]);
        mContext.startActivity(intent);
    }
    @Override
    protected int getContentView() {
        return R.layout.ac_identity_information;
    }

    @Override
    protected void initView() {
        tvIdInfoNext.setClickable(false);
        tvHalfBodyIdInfoNext.setClickable(false);
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, getString(R.string.top_bar_identification));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout), getString(R.string.dialogpic_notice), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
    }

    @Override
    protected void initData() {
        identificationAuthStatus = getIntent().getIntExtra(IDENTIFICATION_AUTH_STATUS,0);
        identificationWithPeopleAuthStatus = getIntent().getIntExtra(IDENTIFICATION_WITH_PEOPLE_AUTH_STATUS,0);
        imageLoader = ImageLoader.getInstance();
        options = MyApplication.getDisplayImageOptions(R.drawable.bg_default_id_card);
        showView();
    }

    /**
     * 显示相应视图
     */
    private void showView(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

            //认证成功
            if(identificationAuthStatus == 1 && identificationWithPeopleAuthStatus == 1) {
                layoutVerifyIdentity.setVisibility(View.GONE);
                layoutVerifyHalfBodyIdentity.setVisibility(View.GONE);
                layoutPromptStatus.setVisibility(View.GONE);
                layoutPromptSuccess.setVisibility(View.VISIBLE);
                imageLoader.displayImage(DataCheckUtil.getMemberObj(mContext).id_number_image.source,ivDriverSuccess);
                imageLoader.displayImage(DataCheckUtil.getMemberObj(mContext).id_number_with_people_image.source,ivDriverWithPeopleSuccess);
            }
            //未认证
            if(identificationAuthStatus == 0 && identificationWithPeopleAuthStatus == 0) {
                layoutVerifyIdentity.setVisibility(View.VISIBLE);
                layoutVerifyHalfBodyIdentity.setVisibility(View.GONE);
                layoutPromptStatus.setVisibility(View.GONE);
                layoutPromptSuccess.setVisibility(View.GONE);
            }
            //认证中
            if(identificationAuthStatus == 2 || identificationWithPeopleAuthStatus == 2) {
                layoutVerifyIdentity.setVisibility(View.GONE);
                layoutVerifyHalfBodyIdentity.setVisibility(View.GONE);
                layoutPromptStatus.setVisibility(View.VISIBLE);
                layoutPromptSuccess.setVisibility(View.GONE);

            }

            }
        });
    }
    @OnClick({ R.id.tvIdInfoTakePhoto, R.id.tvIdInfoNext, R.id.tvHalfBodyIdInfoTakePhoto,R.id.tvHalfBodyIdInfoNext})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvIdInfoTakePhoto: //拍摄身份证
                takePhoto();

                break;
            case R.id.tvIdInfoNext://身份证申请验证
                updateImage();
                break;
            case R.id.tvHalfBodyIdInfoTakePhoto: //拍摄-半身照
                takePhoto();

                break;
            case R.id.tvHalfBodyIdInfoNext: //下一步-半身照
                updateImage();
                break;
        }
    }

    /**
     * 开启拍照
     */
    private void takePhoto(){
        String ImageMessage="时间:"+ TimeUtil.getFormatTime()+"地点:"+""+"经纬度:";
        startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                DO_BIG_IMAGE, 1, 1, 640, 360), UPLOAD_IMG);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 101 && resultCode == TakePhotoActivity.UPLOAD_SUCCESS) {
            if(data != null && data.getExtras() != null) {
                currentImgLocalPath = data.getExtras().getString("PATH");
                //身份证验证
                if (layoutVerifyIdentity.getVisibility() == View.VISIBLE){
                    MyApplication.getDefaultFinalBitmap(mContext).display(ivIdInfo, currentImgLocalPath);
                    //下一步按钮置为可点击状态
                    tvIdInfoNext.setBackgroundResource(R.drawable.bg_btn_green_shape_selector);
                    tvIdInfoNext.setClickable(true);
                    currentImgUploadType = Constants.IMAGE_TYPE_ID_NUM;
                    tvIdInfoTakePhoto.setText("请点击重新拍照");
                }
                //手持身份证验证
                if (layoutVerifyHalfBodyIdentity.getVisibility() == View.VISIBLE){
                    MyApplication.getDefaultFinalBitmap(mContext).display(ivHalfBodyIdInfo, currentImgLocalPath);
                    currentImgUploadType = Constants.IMAGE_TYPE_HAND_ID;
                    //下一步按钮置为可点击状态
                    tvHalfBodyIdInfoNext.setBackgroundResource(R.drawable.bg_btn_green_shape_selector);
                    tvHalfBodyIdInfoNext.setClickable(true);
                    tvHalfBodyIdInfoTakePhoto.setText("请点击重新拍照");
                }

            }
        }
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    /**
     * 上传相应的图片
     * @param
     */
    private void updateImage() {
        if (StringUtils.isBlank(currentImgLocalPath)){
            ToastUtil.showShort(mContext,"请先选择照片");
            return;
        }
        new LHttpLib().editImage(mContext, memberId, currentImgUploadType, currentImgLocalPath, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                final Image image = new Image().parse(jsonStatus.data.optJSONObject("image_info"));
                //身份证情况下暂时不做上传，与半身照一并上传
                if (currentImgUploadType.equals(Constants.IMAGE_TYPE_ID_NUM)){
                    identificationUploadPath = image.source;
                    //进入下一个步骤
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //隐藏身份证验证，显示半身照认证
                            layoutVerifyIdentity.setVisibility(View.GONE);
                            layoutVerifyHalfBodyIdentity.setVisibility(View.VISIBLE);
                            layoutPromptStatus.setVisibility(View.GONE);
                            layoutPromptSuccess.setVisibility(View.GONE);
                        }
                    });
                }else {
                    updateImageInfo( image.source);
                }
            }
        });
    }

    /**
     * 提交身份证和半身照信息相应的信息
     * @param value
     */
    void updateImageInfo(String value) {
        new LHttpLib().submitIdentificationCardInfo(mContext, memberId, identificationUploadPath, value, "", new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                //上传图片成功之后 将 currentImgLocalPath支空
                currentImgLocalPath = "";
                //刷新数据
                DataLoadUtil.refreshMemberData(mContext);
                //进入下一个步骤
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //身份证验证
                        if (layoutVerifyIdentity.getVisibility() == View.VISIBLE){
                            layoutVerifyIdentity.setVisibility(View.GONE);
                            layoutVerifyHalfBodyIdentity.setVisibility(View.VISIBLE);
                            layoutPromptStatus.setVisibility(View.GONE);
                            layoutPromptSuccess.setVisibility(View.GONE);
                        }else {//手持身份证上传成功之后显示审核中界面
                            layoutVerifyIdentity.setVisibility(View.GONE);
                            layoutVerifyHalfBodyIdentity.setVisibility(View.GONE);
                            layoutPromptStatus.setVisibility(View.VISIBLE);
                            layoutPromptSuccess.setVisibility(View.GONE);
                        }
                    }
                });
            }
        });
    }
}
